package com.mco.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：liao.wei
 * @date ：2023/9/20 21:04
 * @package : com.mco.service
 */
@Service
public class GeoService {
	public static final String CITY = "city";
	@Autowired
	private RedisTemplate redisTemplate;
	
	public String geoAdd() {
		Map<String, Point> map = new HashMap<>();
		map.put("天安门", new Point(116.403963, 39.915119));
		map.put("长城", new Point(116.746579, 40.482509));
		map.put("故宫", new Point(116.403414, 39.924091));
		redisTemplate.opsForGeo().add(CITY, map);
		return map.toString();
	}
	
	public Point position(String member) {
		//获取经纬度坐标
		
		return (Point) redisTemplate.opsForGeo().position(CITY, member).get(0);
	}
	
	public String hash(String member) {
		//geohash算法生成的base32编码值
		
		return (String) redisTemplate.opsForGeo().hash(CITY, member).get(0);
	}
	
	public Distance distance(String member1, String member2) {
		// 查询距离
		return redisTemplate.opsForGeo().distance(CITY, member1, member2, RedisGeoCommands.DistanceUnit.KILOMETERS);
	}
	
	public GeoResults radiusByxy() {
		//查询附加
		Circle circle = new Circle(116.403414, 39.924091, Metrics.KILOMETERS.getMultiplier());
		RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortDescending().limit(5);
		return redisTemplate.opsForGeo().radius(CITY, circle, args);
	}
	
	public GeoResults radiusByMember() {
		// 通过地方查询附近
		Distance distance = new Distance(1000, RedisGeoCommands.DistanceUnit.KILOMETERS);
		RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortDescending().limit(5);
		
		return redisTemplate.opsForGeo().radius(CITY, "长城", distance, args);
	}
}
